﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApiV1.Domain;
using WebApiV3.Data;
using WebApiV3.Data.Entity;


namespace WebApiV3.Utils
{
    public class DbInitializer
    {
        public static void Initializer(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin1200DbContext))as Admin1200DbContext;

                var _userRespository = scope.ServiceProvider.GetService(typeof(IRespository<Users>)) as EFRespository<Users>;
                var _roleRespository = scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as EFRespository<Roles>;

                db.Database.EnsureCreated();
                var hasUser = db.Users.Any();
                if (!hasUser)
                {
                    var role = new Roles
                    {
                        RoleName = "管理员",
                    };

                    _roleRespository.Insert(role);

                    _userRespository.InsertBulk(new Users[]
                    {
                    new Users
                    {
                        UserName = "sa",
                        Password = "123456",
                        RolesId = role.Id,
                    },
                    new Users
                    {
                        UserName = "ss",
                        Password = "112233",
                        RolesId = role.Id,
                    }
                    });
                }

                
                


            }
        }
    }
}
